library(pacman)
p_load("tidyverse","lavaan","kableExtra","gridExtra")
load("Data/gssall.RData")
#Methods section: calculate complete observations------------
nrow(na.omit(dplyr::select(all,auth1,auth2,auth3,abortion1, abortion2, abortion3)))
nrow(na.omit(dplyr::select(all,auth1,auth2,auth3, homo1, homo2, homo3)))
nrow(na.omit(dplyr::select(all,auth1,auth2,auth3, polviews_1,  polviews_2,  polviews_3)))
nrow(na.omit(dplyr::select(all,auth1,auth2,auth3, partyid_1,  partyid_2,  partyid_3)))
nrow(na.omit(dplyr::select(all,auth1,auth2,auth3, econatts1,  econatts2,  econatts3)))

## Figure 4: Specify constrained and unconstrained models -----------------------------------------------------------------
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1 + year2008 + year2010
  x3 + y3 ~ x2 + y2 + year2008 + year2010
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ year2008 + year2010
  x1 ~ year2008 + year2010
'

CLPM_c <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ a*x1 + b*y1 + c*year2008 + d*year2010
  x3 + y3 ~ a*x2 + b*y2 + c*year2008 + d*year2010
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ year2008 + year2010
  x1 ~ year2008 + year2010
'

## Figure 4: Abortion ----------------------------------------------------------------
xvars <- colnames(all)[grep("abortion", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
ab_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)
ab_clpm2 <- sem(CLPM_c, data = (gss_short1),missing="ML",fixed.x=F)

## Figure 4: LGBT ----------------------------------------------------------------
xvars <- colnames(all)[grep("homo", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
homo_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)
homo_clpm2 <- sem(CLPM_c, data = (gss_short1),missing="ML",fixed.x=F)
## Figure 4: ideology ----------------------------------------------------------------
xvars <- colnames(all)[grep("polviews", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
polviews_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)
polviews_clpm2 <- sem(CLPM_c, data = (gss_short1),missing="ML",fixed.x=F)
## Figure 4: party ----------------------------------------------------------------
xvars <- colnames(all)[grep("party", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
party_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)
party_clpm2 <- sem(CLPM_c, data = (gss_short1),missing="ML",fixed.x=F)


## Figure 4: Social Welfare ----------------------------------------------------------------
xvars <- colnames(all)[grep("econ", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

econ_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)
econ_clpm2 <- sem(CLPM_c, data = (gss_short1),missing="ML",fixed.x=F)


## Figure 4: prepare data for Figure 4 and Tables ---------------------------------------------------------------
auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(econ_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(homo_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(party_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(polviews_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

## Figure 4: plot results----------------
auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("Abortion",4),rep("Social Welfare",4),rep("LGBT",4),rep("PID",4),rep("Ideology",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplotgss.pdf",width=8,height=6)

## SI 3.6: Test of goodness-of-fit of unconstrained vs. contrained model---------------------------
fitstats <- rbind(data.frame(anova(homo_clpm,homo_clpm2)),
                  data.frame(anova(ab_clpm,ab_clpm2)),
                  data.frame(anova(econ_clpm,econ_clpm2)),
                  data.frame(anova(party_clpm,party_clpm2)),
                  data.frame(anova(polviews_clpm,polviews_clpm2)))

fitstats$constrained <- c("Unconstrained","Constrained")
fitstats <- fitstats %>% relocate(constrained)

kbl(fitstats,booktabs = T, digits=2, caption="Fit Statistics for Unconstrained versus Constrained Models", format = "latex", escape = F,row.names = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "LGBT") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Abortion") %>%
  kableExtra::group_rows(start_row = 5,end_row = 6,group_label = "Economic Attitudes") %>%
  kableExtra::group_rows(start_row = 7,end_row = 8,group_label = "Party ID") %>%
  kableExtra::group_rows(start_row = 9,end_row = 10,group_label = "Ideology") -> fitstats
fitstats %>% save_kable("Results/fitstatsgss.tex")

## SI 3.5: Results Figure 4: Aborition attitudes ---------------------------------------------------------------
parameterEstimates(ab_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% select(c(1,3:5,7)) %>% slice(1,4,5,8) -> libcon_param
libcon_param$pvalue <- libcon_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(ab_clpm)[names(fitmeasures(ab_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(ab_clpm)[names(fitmeasures(ab_clpm)) ==
                                              "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(ab_clpm)[names(fitmeasures(ab_clpm)) ==
                                        "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(ab_clpm)[names(fitmeasures(ab_clpm)) ==
                                        "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(ab_clpm)[names(fitmeasures(ab_clpm)) ==
                                         "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(libcon_param)
ab_clpm <- rbind(libcon_param[1:4,],fitindicies)
ab_clpm[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), ab_clpm[1:4,3:5])

ab_clpm$lhs <- c("Politics", "Personality", "Politics","Personality","RMSEA","CFI","SRMR")
ab_clpm$rhs <- c("$Personality_{t-1}$", "$Politics_{t-1}$", "$Personality_{t-1}$", "$Politics_{t-1}$",ab_clpm$rhs[5:7])
colnames(ab_clpm) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
options(knitr.kable.NA = '')

kbl(ab_clpm,booktabs = T, digits=2, caption="Cross-lagged coefficients between Abortion Attitudes and Authoritarianism", format = "latex", escape = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") ->  ab_tab
ab_tab %>%  kable_styling(latex_options = c( "hold_position")) %>% save_kable("Results/gssabortion.tex")

## SI 3.5: Results Figure 4: LBGT attitudes ---------------------------------------------------------------
parameterEstimates(homo_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% select(c(1,3:5,7)) %>% slice(1,4,5,8)-> eu_param
eu_param$pvalue <- eu_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(homo_clpm)[names(fitmeasures(homo_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(homo_clpm)[names(fitmeasures(homo_clpm)) ==
                                                "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(homo_clpm)[names(fitmeasures(homo_clpm)) ==
                                          "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(homo_clpm)[names(fitmeasures(homo_clpm)) ==
                                          "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(homo_clpm)[names(fitmeasures(homo_clpm)) ==
                                           "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(eu_param)
eu_tab <- rbind(eu_param[1:4,],fitindicies)
eu_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), eu_tab[1:4,3:5])

eu_tab$lhs <- c("Politics", "Personality", "Politics","Personality","RMSEA","CFI","SRMR")
eu_tab$rhs <- c("$Personality_{t-1}$", "$Politics_{t-1}$", "$Personality_{t-1}$", "$Politics_{t-1}$",eu_tab$rhs[5:7])
colnames(eu_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
options(knitr.kable.NA = '')
kbl(eu_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between LGBT Attitudes and Closed Personality", format = "latex", escape = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") ->  homo_tab
homo_tab %>%  kable_styling(latex_options = c( "hold_position")) %>% save_kable("Results/gsslbt.tex")

## SI 3.5: Results Figure 4: Ideology ---------------------------------------------------------------
parameterEstimates(polviews_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% select(c(1,3:5,7)) %>% slice(1,4,5,8)-> income_param
income_param$pvalue <- income_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(polviews_clpm)[names(fitmeasures(polviews_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(polviews_clpm)[names(fitmeasures(polviews_clpm)) ==
                                                    "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(polviews_clpm)[names(fitmeasures(polviews_clpm)) ==
                                              "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(polviews_clpm)[names(fitmeasures(polviews_clpm)) ==
                                              "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(polviews_clpm)[names(fitmeasures(polviews_clpm)) ==
                                               "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(income_param)
income_tab <- rbind(income_param[1:4,],fitindicies)
income_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), income_tab[1:4,3:5])


income_tab$lhs <- c("Politics", "Personality", "Politics","Personality","RMSEA","CFI","SRMR")
income_tab$rhs <- c("$Personality_{t-1}$", "$Politics_{t-1}$", "$Personality_{t-1}$", "$Politics_{t-1}$",income_tab$rhs[5:7])
colnames(income_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
options(knitr.kable.NA = '')

kbl(income_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Liberal-Conservative Self-Placement and Closed Personality", format = "latex", escape = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") ->  polviews_tab
polviews_tab %>%  kable_styling(latex_options = c( "hold_position")) %>% save_kable("Results/gsspolviews.tex")

## SI 3.5: Results Figure 4: Partisanship ---------------------------------------------------------------
parameterEstimates(party_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% select(c(1,3:5,7)) %>% slice(1,4,5,8)-> immi_param
immi_param$pvalue <- immi_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(party_clpm)[names(fitmeasures(party_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(party_clpm)[names(fitmeasures(party_clpm)) ==
                                                 "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(party_clpm)[names(fitmeasures(party_clpm)) ==
                                           "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(party_clpm)[names(fitmeasures(party_clpm)) ==
                                           "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(party_clpm)[names(fitmeasures(party_clpm)) ==
                                            "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(immi_param)
immi_tab <- rbind(immi_param[1:4,],fitindicies)
immi_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), immi_tab[1:4,3:5])

immi_tab$lhs <- c("Politics", "Personality", "Politics","Personality","RMSEA","CFI","SRMR")
immi_tab$rhs <- c("$Personality_{t-1}$", "$Politics_{t-1}$", "$Personality_{t-1}$", "$Politics_{t-1}$",immi_tab$rhs[5:7])
colnames(immi_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
options(knitr.kable.NA = '')
kbl(immi_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Partisanship and Authoritarianism", format = "latex", escape = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") ->  party_tab
party_tab %>%  kable_styling(latex_options = c( "hold_position")) %>% save_kable("Results/gssparty.tex")

## SI 3.5: Results Figure 4: Social welfare ---------------------------------------------------------------
parameterEstimates(econ_clpm) %>% filter((str_detect(lhs, "^x") & str_detect(rhs, "^y")) | (str_detect(lhs, "^y") & str_detect(rhs, "^x")) ) %>% select(c(1,3:5,7)) %>% slice(1,4,5,8) -> euth_param
euth_param$pvalue <- euth_param$pvalue/2
fitindicies <-
  rbind(c(
    "RMSEA",
    round(fitmeasures(econ_clpm)[names(fitmeasures(econ_clpm)) == "rmsea"], 2),
    paste0("CI:",round(fitmeasures(econ_clpm)[names(fitmeasures(econ_clpm)) ==
                                                "rmsea.ci.lower"], 2),"-",
           round(fitmeasures(econ_clpm)[names(fitmeasures(econ_clpm)) ==
                                          "rmsea.ci.upper"], 2)),NA, NA
  ),
  c("CFI", round(fitmeasures(econ_clpm)[names(fitmeasures(econ_clpm)) ==
                                          "cfi"], 2), rep(NA, 3)),
  c("SRMR", round(fitmeasures(econ_clpm)[names(fitmeasures(econ_clpm)) ==
                                           "srmr"], 2), rep(NA, 3)))
colnames(fitindicies) <- colnames(euth_param)
euth_tab <- rbind(euth_param[1:4,],fitindicies)
euth_tab[1:4,3:5] <- mapply(function(x)as.character(formatC(round(as.numeric(x),2),2,format="f")), euth_tab[1:4,3:5])
euth_tab$lhs <- c("Politics", "Personality", "Politics","Personality","RMSEA","CFI","SRMR")
euth_tab$rhs <- c("$Personality_{t-1}$", "$Politics_{t-1}$", "$Personality_{t-1}$", "$Politics_{t-1}$",euth_tab$rhs[5:7])
colnames(euth_tab) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")

kbl(euth_tab,booktabs = T, digits=2, caption="Cross-lagged coefficients between Social Welfare Attitudes and Closed Personality", format = "latex", escape = F) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 5,end_row = 7,group_label = "Fit Indices") %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Wave 2") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "Wave 3") ->  econ_tab
econ_tab %>%  kable_styling(latex_options = c( "hold_position")) %>% save_kable("Results/gssecon.tex")

## SI 3.2: descriptive statistics of measures -----------
w1 <- mapply(zero1,with(gssall,data.frame(auth1, abortion1, homo1,polviews_1,partyid_1,  econatts1)))
w2 <- mapply(zero1,with(gssall,data.frame(auth2, abortion2, homo2,polviews_2,partyid_2,  econatts2)))
w3 <- mapply(zero1,with(gssall,data.frame(auth3, abortion3, homo3,polviews_3,partyid_3,  econatts3)))

reshape2::melt(data.frame(w1)) -> w1a #ignore warning
w1a %>% group_by(variable) %>% summarise(mean=mean(value,na.rm=T),sd=sd(value,na.rm=T),sum(value, na.rm=T)) -> w1a
reshape2::melt(data.frame(w2)) -> w2a #ignore warning
w2a %>% group_by(variable) %>% summarise(mean=mean(value,na.rm=T),sd=sd(value,na.rm=T),sum(value, na.rm=T)) -> w2a
reshape2::melt(data.frame(w3)) -> w3a #ignore warning
w3a %>% group_by(variable) %>% summarise(mean=mean(value,na.rm=T),sd=sd(value,na.rm=T),sum(value, na.rm=T)) -> w3a

alldescr <- rbind(w1a,w2a,w3a)
alldescr <- data.frame(wave=c("Wave 1", "Wave 1", "Wave 1", "Wave 1", "Wave 1", "Wave 1", "Wave 2", "Wave 2", "Wave 2", "Wave 2", "Wave 2", "Wave 2", "Wave 3", "Wave 3", "Wave 3", "Wave 3", "Wave 3", "Wave 3"), variable=c("Authoritarianism",  "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare", "Authoritarianism",  "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare", "Authoritarianism",  "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare"),alldescr[,-1])
colnames(alldescr)<- c("Wave", "Variable", "M", "SD", "N")

print(xtable(alldescr, caption="Descriptive Statistics of Measures by Wave", digits=c(0,0,0, 2,2,0),align=c("llcccc")), label = "tab:gss_descriptives",  include.rownames=F,caption.placement="top", file="Results/appendix_gss_descri.tex")

## SI 3.3: psychometric properties latent traits---------------
wave1psych <-
  list(
    psych::alpha(data.frame((6-gss2006$obey_1), gss2006$thnkself_1))[[1]],
    psych::alpha(data.frame(zero1(gss2006$abany_1),zero1(gss2006$abrape_1),zero1(gss2006$abdefect_1),zero1(gss2006$abhlth_1),zero1(gss2006$abnomore_1),zero1(gss2006$abpoor_1),zero1(gss2006$absingle_1)))[[1]],
    psych::alpha(data.frame(zero1(gss2006$homo1),zero1(gss2006$homo2),zero1(gss2006$homo3)))[[1]],
    psych::alpha(data.frame(gss2006$helppoor_1,gss2006$helpsick_1,gss2006$eqwlth_1, gss2006$helpnot_1))[[1]]
  )

wave2psych <-
  list(
    psych::alpha(data.frame((6-gss2008$obey_1), gss2008$thnkself_1))[[1]],
    psych::alpha(data.frame(zero1(gss2008$abany_1),zero1(gss2008$abrape_1),zero1(gss2008$abdefect_1),zero1(gss2008$abhlth_1),zero1(gss2008$abnomore_1),zero1(gss2008$abpoor_1),zero1(gss2008$absingle_1)))[[1]],
    psych::alpha(data.frame(zero1(gss2008$spkhomo_1),zero1(gss2008$colhomo_1),zero1(3-gss2008$libhomo_1)))[[1]],
    psych::alpha(data.frame(gss2008$helppoor_1,gss2008$helpsick_1,gss2008$eqwlth_1, gss2008$helpnot_1))[[1]]
  )

wave3psych <-
  list(psych::alpha(data.frame((6-gss2010$obey_1), gss2010$thnkself_1))[[1]],
       psych::alpha(data.frame(zero1(gss2010$abany_1),zero1(gss2010$abrape_1),zero1(gss2010$abdefect_1),zero1(gss2010$abhlth_1),zero1(gss2010$abnomore_1),zero1(gss2010$abpoor_1),zero1(gss2010$absingle_1)))[[1]],
       psych::alpha(data.frame(zero1(gss2010$spkhomo_1),zero1(gss2010$colhomo_1),zero1(3-gss2010$libhomo_1)))[[1]],
       psych::alpha(data.frame(gss2010$helppoor_1,gss2010$helpsick_1,gss2010$eqwlth_1, gss2010$helpnot_1))[[1]]
  )

wave1 <- do.call("rbind",wave1psych)[,c(2,4)]
wave2 <- do.call("rbind",wave2psych)[,c(2,4)]
wave3 <- do.call("rbind",wave3psych)[,c(2,4)]

alphas <- rbind(data.frame(wave="Wave 1", trait= NA ,wave1),
                data.frame(wave="Wave 2", trait= NA ,wave2),
                data.frame(wave="Wave 3", trait= NA ,wave3))
alphas$trait <- rep(c("Authoritarianism","Abortion","LGBT","Welfare"),3)
colnames(alphas) <- c("Wave","Trait","Alpha","Average r")
print(xtable(alphas, caption="Reliability of Measures by Wave", digits=c(0,0,0, 2,2), align=c("llccc")),include.rownames=F,caption.placement="top",file="Results/appendix_gss_alphas.tex")

## SI 3.4: correlation between variables----------------
w1cor <- cor(w1,use="pairwise.complete.obs")
rownames(w1cor) <- c("Authoritarianism", "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare")
colnames(w1cor) <- c("Authoritarianism", "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare")
a<-ggcorrplot(w1cor, type="lower", show.diag = TRUE, lab=TRUE, lab_size = 2 , title="A. Measures from Wave 1")

w2cor <- cor(w2,use="pairwise.complete.obs")
rownames(w2cor) <- c("Authoritarianism", "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare")
colnames(w2cor) <- c("Authoritarianism", "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare")
b<-ggcorrplot(w2cor, type="lower", show.diag = TRUE, lab=TRUE, lab_size = 2 , title="B. Measures from Wave 2")

w3cor <- cor(w3,use="pairwise.complete.obs")
rownames(w3cor) <- c("Authoritarianism", "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare")
colnames(w3cor) <- c("Authoritarianism", "Abortion", "LGBT", "Conservatism", "Partisanship", "Welfare")
c<-ggcorrplot(w3cor, type="lower", show.diag = TRUE, lab=TRUE, lab_size = 2 , title="C. Measures from Wave 3")

corplot <- grid.arrange(a,b, c)
ggsave(corplot,file="Results/gss_correlations.pdf", height=15, width=15)


## SI 3.7: select variables for RI-CPLM------------
riclpmtext <- riclpm_text(var_groups = list(x=c("x1","x2","x3"),y=c("y1","y2","y3")))
## SI 3.7: RI-CPLM Abortion -----------
xvars <- colnames(all)[grep("abortion", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
gss_short1$x1<- scale(gss_short1$x1)
gss_short1$x2<- scale(gss_short1$x2)
gss_short1$x3<- scale(gss_short1$x3)
gss_short1$y1<- scale(gss_short1$y1)
gss_short1$y2<- scale(gss_short1$y2)
gss_short1$y3<- scale(gss_short1$y3)
abortion_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short1,missing="ML")
parameterEstimates(abortion_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> riclpmliss
abcov <- parameterEstimates(abortion_tab)[22:23,]
riclpmliss$pvalue <- riclpmliss$pvalue/2
riclpmliss$dv <- c("Politics","Personality")
riclpmliss$iv <- c("$Personality_{t-1}$", "$Politics_{t-1}$")
riclpmliss %>% select(dv,iv,est,se,pvalue) -> riclpmliss
colnames(riclpmliss) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
ab <- riclpmliss

## SI 3.7: RI-CPLM LGBT -----------
xvars <- colnames(all)[grep("homo", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars) -> gss_short1

names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$x1<- scale(gss_short1$x1)
gss_short1$x2<- scale(gss_short1$x2)
gss_short1$x3<- scale(gss_short1$x3)
gss_short1$y1<- scale(gss_short1$y1)
gss_short1$y2<- scale(gss_short1$y2)
gss_short1$y3<- scale(gss_short1$y3)

abortion_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short1,missing="ML")
lgbtcov <- parameterEstimates(abortion_tab)[22:23,]

parameterEstimates(abortion_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> riclpmliss
riclpmliss$pvalue <- riclpmliss$pvalue/2
riclpmliss$dv <- c("Politics","Personality")
riclpmliss$iv <- c("$Personality_{t-1}$", "$Politics_{t-1}$")
riclpmliss %>% select(dv,iv,est,se,pvalue) -> riclpmliss
colnames(riclpmliss) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
lgbt <- riclpmliss

## SI 3.7: RI-CPLM Ideology -----------
xvars <- colnames(all)[grep("polviews", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars) -> gss_short1

names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$x1<- scale(gss_short1$x1)
gss_short1$x2<- scale(gss_short1$x2)
gss_short1$x3<- scale(gss_short1$x3)
gss_short1$y1<- scale(gss_short1$y1)
gss_short1$y2<- scale(gss_short1$y2)
gss_short1$y3<- scale(gss_short1$y3)

abortion_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short1,missing="ML")

parameterEstimates(abortion_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> riclpmliss
riclpmliss$pvalue <- riclpmliss$pvalue/2
riclpmliss$dv <- c("Politics","Personality")
riclpmliss$iv <- c("$Personality_{t-1}$", "$Politics_{t-1}$")
riclpmliss %>% select(dv,iv,est,se,pvalue) -> riclpmliss
colnames(riclpmliss) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
ideo <- riclpmliss

## SI 3.7: RI-CPLM Partisanship -----------
xvars <- colnames(all)[grep("party", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars) -> gss_short1

names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$x1<- scale(gss_short1$x1)
gss_short1$x2<- scale(gss_short1$x2)
gss_short1$x3<- scale(gss_short1$x3)
gss_short1$y1<- scale(gss_short1$y1)
gss_short1$y2<- scale(gss_short1$y2)
gss_short1$y3<- scale(gss_short1$y3)

abortion_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short1,missing="ML")
partycov <- parameterEstimates(abortion_tab)[22:23,]


parameterEstimates(abortion_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> riclpmliss
riclpmliss$pvalue <- riclpmliss$pvalue/2
riclpmliss$dv <- c("Politics","Personality")
riclpmliss$iv <- c("$Personality_{t-1}$", "$Politics_{t-1}$")
riclpmliss %>% select(dv,iv,est,se,pvalue) -> riclpmliss
colnames(riclpmliss) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
party <- riclpmliss

## SI 3.7: RI-CPLM Social Welfare-----------
xvars <- colnames(all)[grep("econ", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars) -> gss_short1

names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$x1<- scale(gss_short1$x1)
gss_short1$x2<- scale(gss_short1$x2)
gss_short1$x3<- scale(gss_short1$x3)
gss_short1$y1<- scale(gss_short1$y1)
gss_short1$y2<- scale(gss_short1$y2)
gss_short1$y3<- scale(gss_short1$y3)

abortion_tab <- riclpmr::lavriclpm(riclpmtext,data = gss_short1,missing="ML")
swcov <- parameterEstimates(abortion_tab)[22:23,]

parameterEstimates(abortion_tab) %>% filter(label %in% c("x_y","y_x"))%>%  slice(1,3) -> riclpmliss
riclpmliss$pvalue <- riclpmliss$pvalue/2
riclpmliss$dv <- c("Politics","Personality")
riclpmliss$iv <- c("$Personality_{t-1}$", "$Politics_{t-1}$")
riclpmliss %>% select(dv,iv,est,se,pvalue) -> riclpmliss
colnames(riclpmliss) <- c("Outcome Measure","Predictor","$\\beta$","SE","p-value")
sw <- riclpmliss

## SI 3.7: built table --------------
riclpmliss <- rbind(ab, lgbt,sw,party,ideo)
names(riclpmliss) <- c("dv","iv","est","se","pvalue")

kbl(riclpmliss,booktabs = T, digits=2, caption="Cross-lagged coefficients from RI-CLPM models in GSS data", format = "latex",escape = F) %>% kable_styling(latex_options = c( "hold_position")) %>%
  kable_classic(full_width = F) %>%
  kableExtra::group_rows(start_row = 1,end_row = 2,group_label = "Abortion Attitudes") %>%
  kableExtra::group_rows(start_row = 3,end_row = 4,group_label = "LGBT Attitudes") %>%
  kableExtra::group_rows(start_row = 5,end_row = 6,group_label = "Social Welfare Attitudes") %>%
  kableExtra::group_rows(start_row = 7,end_row = 8,group_label = "Party ID") %>%
  kableExtra::group_rows(start_row = 9,end_row = 10,group_label = "Ideology") %>%
save_kable("Results/gssRICLPM.tex")



## SI 3.8 Per year: create model------------
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1
  x3 + y3 ~ x2 + y2
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
'

## SI 3.8 Per Year: Abortion ----------------------------------------------------------------
xvars <- colnames(all)[grep("abortion", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]

all %>% select(xvars, yvars,id,year, year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ab_clpm_06 <- sem(CLPM_u, subset(gss_short1, year==2006),missing="ML",fixed.x=F)
ab_clpm_08 <- sem(CLPM_u, subset(gss_short1, year==2008),missing="ML",fixed.x=F)
ab_clpm_10 <- sem(CLPM_u, subset(gss_short1, year==2010),missing="ML",fixed.x=F)

auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm_06,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_08,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_10,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("2006",4),rep("2008",4),rep("2010",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplot_aborition_year.pdf",width=8,height=6)

## SI 3.8 Per Year: LGBT ----------------------------------------------------------------
xvars <- colnames(all)[grep("homo", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]

all %>% select(xvars, yvars,id,year, year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ab_clpm_06 <- sem(CLPM_u, subset(gss_short1, year==2006),missing="ML",fixed.x=F)
ab_clpm_08 <- sem(CLPM_u, subset(gss_short1, year==2008),missing="ML",fixed.x=F)
ab_clpm_10 <- sem(CLPM_u, subset(gss_short1, year==2010),missing="ML",fixed.x=F)

auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm_06,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_08,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_10,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("2006",4),rep("2008",4),rep("2010",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplot_lgbt_year.pdf",width=8,height=6)

## SI 3.8 Per Year: social welfare ----------------------------------------------------------------
xvars <- colnames(all)[grep("econ", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]

all %>% select(xvars, yvars,id,year, year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ab_clpm_06 <- sem(CLPM_u, subset(gss_short1, year==2006),missing="ML",fixed.x=F)
ab_clpm_08 <- sem(CLPM_u, subset(gss_short1, year==2008),missing="ML",fixed.x=F)
ab_clpm_10 <- sem(CLPM_u, subset(gss_short1, year==2010),missing="ML",fixed.x=F)

auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm_06,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_08,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_10,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("2006",4),rep("2008",4),rep("2010",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplot_socialwelfare_year.pdf",width=8,height=6)

## SI 3.8 Per Year: Party ----------------------------------------------------------------
xvars <- colnames(all)[grep("party", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]

all %>% select(xvars, yvars,id,year, year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ab_clpm_06 <- sem(CLPM_u, subset(gss_short1, year==2006),missing="ML",fixed.x=F)
ab_clpm_08 <- sem(CLPM_u, subset(gss_short1, year==2008),missing="ML",fixed.x=F)
ab_clpm_10 <- sem(CLPM_u, subset(gss_short1, year==2010),missing="ML",fixed.x=F)

auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm_06,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_08,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_10,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("2006",4),rep("2008",4),rep("2010",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplot_party_year.pdf",width=8,height=6)

## SI 3.8 Per Year: Ideology ----------------------------------------------------------------
xvars <- colnames(all)[grep("polviews", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]

all %>% select(xvars, yvars,id,year, year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ab_clpm_06 <- sem(CLPM_u, subset(gss_short1, year==2006),missing="ML",fixed.x=F)
ab_clpm_08 <- sem(CLPM_u, subset(gss_short1, year==2008),missing="ML",fixed.x=F)
ab_clpm_10 <- sem(CLPM_u, subset(gss_short1, year==2010),missing="ML",fixed.x=F)

auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm_06,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_08,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_10,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("2006",4),rep("2008",4),rep("2010",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplot_ideology_year.pdf",width=8,height=6)


## SI 3.9 Models with controls: specify unonstrained model --------
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1 + year2008 + year2010 + degree_1_1 + degree_1_2 + degree_1_3 +degree_1_4 + rincome4_1 + rincome4_2 + rincome4_3 + rincome4_4 + age_1 + sex_1 + race_1_2 + race_1_3
  x3 + y3 ~ x2 + y2 + year2008 + year2010 + degree_1_1 + degree_1_2 + degree_1_3 +degree_1_4 + rincome4_1 + rincome4_2 + rincome4_3 + rincome4_4 + age_1 + sex_1 + race_1_2 + race_1_3
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ year2008 + year2010
  x1 ~ year2008 + year2010
'
## SI 3.9 Models with controls:: Abortion ----------------------------------------------------------------
xvars <- colnames(all)[grep("abortion", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
ab_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)

## SI 3.9 Models with controls:: LGBT ----------------------------------------------------------------
xvars <- colnames(all)[grep("homo", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
homo_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)


## SI 3.9 Models with controls:: social welfare ----------------------------------------------------------------
xvars <- colnames(all)[grep("econ", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
econ_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)

## SI 3.9 Models with controls:: party ----------------------------------------------------------------
xvars <- colnames(all)[grep("party", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
party_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F,meanstructure = T)

## SI 3.9 Models with controls:: polviews ----------------------------------------------------------------
xvars <- colnames(all)[grep("polviews", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
polviews_clpm <- sem(CLPM_u, data = (gss_short1),missing="ML",fixed.x=F)

## SI 3.9 Models with controls: create figure ---------------------------------------------------------------

auth_results <-
  gtools::smartbind(
    parameterestimates(ab_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(econ_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(homo_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(party_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(polviews_clpm,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results$lhs <- c("DV: Political Preference","DV: Authoritarianism")
auth_results$lhs <- relevel(factor(auth_results$lhs),ref="DV: Political Preference")
auth_results$rhs <- c("IV: Authoritarianism","IV: Political Preference")
auth_results$label <- c(rep("Abortion",4),rep("Social Welfare",4),rep("LGBT",4),rep("PID",4),rep("Ideology",4))
auth_results$wave <- c(rep("Wave 2 DV~Wave 1 IV",2),rep("Wave 3 DV~Wave 2 IV",2))

ggplot(auth_results,aes(x=label,y=est,shape=wave))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=.5))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw(base_size = 15)+labs(shape="Model")+ylab("Standardized Coefficient")+theme(axis.text.x = element_text())+xlab("Policy Variable") + theme(legend.position="bottom") +theme(axis.text.x = element_text(angle = 45,hjust=1))
ggsave("Results/xlaggedplotgsswithcontrols.pdf",width=8,height=6)


## SI 9.1 Age: specify model-------------

CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1 + year2008 + year2010
  x3 + y3 ~ x2 + y2 + year2008 + year2010
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ year2008 + year2010
  x1 ~ year2008 + year2010
'

## SI 9.1 Age: Abortion ----------------------------------------------------------------
xvars <- colnames(all)[grep("abortion", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$age1_median<-ifelse(gss_short1$age_1>median(gss_short1$age_1, na.rm=T), 1,0)
ab_clpm_young <- sem(CLPM_u, data = subset(gss_short1, age1_median==0),missing="ML",fixed.x=F)
ab_clpm_old <- sem(CLPM_u, data = subset(gss_short1, age1_median==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
    parameterestimates(ab_clpm_young,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
    parameterestimates(ab_clpm_old,level = .90) %>% filter(
      (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Abortion",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Young",4),rep("Old",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Young")

plot_abortion <- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("A. Abortion")


## SI 9.1 Age:  LGBT-----------------------
xvars <- colnames(all)[grep("homo", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$age1_median<-ifelse(gss_short1$age_1>median(gss_short1$age_1, na.rm=T), 1,0)
homo_clpm_young <- sem(CLPM_u, data = subset(gss_short1, age1_median==0),missing="ML",fixed.x=F)
homo_clpm_old <- sem(CLPM_u, data = subset(gss_short1, age1_median==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(homo_clpm_young,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(homo_clpm_old,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("LBTQ",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Young",4),rep("Old",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Young")

plot_lgbt<- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("B. LGBT")

## SI 9.1 Age:  polviews---------------------------
xvars <- colnames(all)[grep("polviews", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

gss_short1$age1_median<-ifelse(gss_short1$age_1>median(gss_short1$age_1, na.rm=T), 1,0)
ideology_clpm_young <- sem(CLPM_u, data = subset(gss_short1, age1_median==0),missing="ML",fixed.x=F)
ideology_clpm_old <- sem(CLPM_u, data = subset(gss_short1, age1_median==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(ideology_clpm_young,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(ideology_clpm_old,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Conservatism",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Young",4),rep("Old",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Young")

plot_conservatism<- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("C. Consveratism")

## SI 9.1 Age: party----------------------
xvars <- colnames(all)[grep("party", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
gss_short1$age1_median<-ifelse(gss_short1$age_1>median(gss_short1$age_1, na.rm=T), 1,0)
party_clpm_young <- sem(CLPM_u, data = subset(gss_short1, age1_median==0),missing="ML",fixed.x=F)
party_clpm_old <- sem(CLPM_u, data = subset(gss_short1, age1_median==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(party_clpm_young,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(party_clpm_old,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))
auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Partisanship",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Young",4),rep("Old",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Young")

plot_pid<- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("D. Partisanship")

## SI 9.1 Age: Welfare-------------------
xvars <- colnames(all)[grep("econ", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,degree_1,age_1,degree_2,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)
gss_short1$age1_median<-ifelse(gss_short1$age_1>median(gss_short1$age_1, na.rm=T), 1,0)
welfare_clpm_young <- sem(CLPM_u, data = subset(gss_short1, age1_median==0),missing="ML",fixed.x=F)
welfare_clpm_old <- sem(CLPM_u, data = subset(gss_short1, age1_median==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(welfare_clpm_young,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(welfare_clpm_old,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Welfare",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Young",4),rep("Old",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Young")

plot_welfare<- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("E. Welfare")

## SI 9.1 Age: create plot----------------
#create plot
comb_plot<-grid.arrange(plot_abortion, plot_lgbt, plot_conservatism, plot_pid, plot_welfare, nrow = 5)
ggsave(comb_plot, file="Results/GSS_conditional_age.pdf",width=8,height=9)

## SI 9.2 Education: build model----------------------
CLPM_u <- '
  # Estimate the lagged effects between the observed variables.
  x2 + y2 ~ x1 + y1 + year2008 + year2010
  x3 + y3 ~ x2 + y2 + year2008 + year2010
  x3 ~~ x3
  x2 ~~ x2
  x1 ~~ x1
  y3 ~~ y3
  y2 ~~ y2
  y1 ~~ y1
  y3 ~~ x3
  y2 ~~ x2
  y1 ~~ x1
  y1 ~ year2008 + year2010
  x1 ~ year2008 + year2010
'


## SI 9.2 Education: Abortion ----------------------------------------------------------------
xvars <- colnames(all)[grep("abortion", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,age_1, sex_2,age_2,white1,white2,edu01,starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ab_clpm_low <- sem(CLPM_u, data = subset(gss_short1, edu01==0),missing="ML",fixed.x=F)
ab_clpm_high <- sem(CLPM_u, data = subset(gss_short1, edu01==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(ab_clpm_low,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(ab_clpm_high,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))


auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Abortion",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Low",4),rep("High",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Low")

plot_abortion <- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("A. Abortion")

## SI 9.2 Education: LTBQ--------------
xvars <- colnames(all)[grep("homo", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,age_1, edu01, age_1,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

homo_clpm_low <- sem(CLPM_u, data = subset(gss_short1, edu01==0),missing="ML",fixed.x=F)
homo_clpm_high <- sem(CLPM_u, data = subset(gss_short1, edu01==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(homo_clpm_low,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(homo_clpm_high,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))
auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("LGBT",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Low",4),rep("High",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Low")

plot_lgbt <- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("B. LGBT")

## SI 9.2 Education: polviews---------------------------
xvars <- colnames(all)[grep("polviews", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,age_1, edu01, age_1,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

ideology_clpm_low <- sem(CLPM_u, data = subset(gss_short1, edu01==0),missing="ML",fixed.x=F)
ideology_clpm_high <- sem(CLPM_u, data = subset(gss_short1, edu01==1),missing="ML",fixed.x=F)
summary(ideology_clpm_high)
auth_results_age <- gtools::smartbind(
  parameterestimates(ideology_clpm_low,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(ideology_clpm_high,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))
auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Conservatism",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Low",4),rep("High",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Low")

plot_conservatism <- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("C. Conservatism")

## SI 9.2 Education: party--------------------------
xvars <- colnames(all)[grep("party", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,age_1, edu01, age_1,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1
names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

pid_cplm_low <- sem(CLPM_u, data = subset(gss_short1, edu01==0),missing="ML",fixed.x=F)
pid_clpm_high <- sem(CLPM_u, data = subset(gss_short1, edu01==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(pid_cplm_low,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(pid_clpm_high,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))
auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Partisanship",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Low",4),rep("High",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Low")

plot_pid <- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("D. Partisanship")

## SI 9.2 Education: Welfare-----------------------------
xvars <- colnames(all)[grep("econ", x = colnames(all))][c(1,2,3)]
yvars <- colnames(all)[grep("auth", x = colnames(all))][c(1,2,3)]
all %>% select(xvars, yvars,id,year2008,year2010,sex_1,age_1, edu01, age_1,sex_2,age_2,white1,white2,starts_with("degree"),starts_with("race"),starts_with("rinc")) -> gss_short1

names(gss_short1)[1:3] <- paste0("x", 1:3)
names(gss_short1)[4:6] <- paste0("y", 1:3)

welfare_cplm_low <- sem(CLPM_u, data = subset(gss_short1, edu01==0),missing="ML",fixed.x=F)
welfare_clpm_high <- sem(CLPM_u, data = subset(gss_short1, edu01==1),missing="ML",fixed.x=F)

auth_results_age <- gtools::smartbind(
  parameterestimates(welfare_cplm_low,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")),
  parameterestimates(welfare_clpm_high,level = .90) %>% filter(
    (lhs=="x2" & rhs=="y1")|(lhs=="x3" & rhs=="y2")|(lhs=="y2" & rhs=="x1")|(lhs=="y3" & rhs=="x2")))

auth_results_age$lhs <- c("DV: Policy Preference","DV: Authoritarianism")
auth_results_age$lhs <- relevel(factor(auth_results_age$lhs),ref="DV: Policy Preference")
auth_results_age$rhs <- c("IV: Authoritarianism","IV: Policy Preference")
auth_results_age$label <- c(rep("Welfare",4))
auth_results_age$model <- c(rep("Wave 2 ~ Wave 1",2),rep("Wave 3 ~ Wave 2",2))
auth_results_age$age <- c(rep("Low",4),rep("High",4))
auth_results_age$age <- relevel(factor(auth_results_age$age),ref="Low")

plot_welfare <- ggplot(auth_results_age,aes(x=label,y=est,shape=model, color=age))+geom_pointrange(aes(ymin=ci.lower,ymax=ci.upper),position=position_dodge(width=1))+facet_wrap(lhs~rhs)+geom_hline(yintercept = 0,lty=2)+theme_bw() + theme(legend.position = "none") + labs(x = "", y="Standardized Coefficient") + ggtitle("E. Welfare")

## SI 9.2 Education: create plot----------------------
comb_plot<-grid.arrange(plot_abortion, plot_lgbt, plot_conservatism, plot_pid, plot_welfare, nrow = 5)
ggsave(comb_plot, file="Results/GSS_conditional_education.pdf",width=8,height=9)


